clear
clear matrix
set mem 300m

insheet using "$sourcedatadir/wtid/oecd_uer.csv"
sort country
save "$gendatadir/oecd_uer.dta", replace

clear
insheet using "$sourcedatadir/wtid/view_election_pres.csv"
drop if party_name_short != "Dem" & party_name_short != "Rep"
bysort election_id: egen vote_total_2party = total(vote_share)
gen vote_share_2party = 100 * vote_share / vote_total_2party
replace vote_share = vote_share_2party
drop vote_total_2party vote_share_2party
drop if country_name == "France"
save "$gendatadir/view_election_pres.dta", replace

clear
insheet using "$sourcedatadir/wtid/view_cabinet.csv"
save "$gendatadir/view_cabinet.dta", replace

clear
insheet using "$sourcedatadir/wtid/view_cabinet_pres.csv"
drop if country_name == "France"
save "$gendatadir/view_cabinet_pres.dta", replace

clear
insheet using "$sourcedatadir/wtid/view_party_pres.csv"
save "$gendatadir/view_party_pres.dta", replace

clear
insheet using "$sourcedatadir/wtid/view_party.csv"
append  using "$gendatadir/view_party_pres.dta"
save "$gendatadir/view_party.dta", replace

clear
insheet using "$sourcedatadir/wtid/view_election.csv"
append  using "$gendatadir/view_election_pres.dta"
save "$gendatadir/view_election.dta", replace

clear
insheet using "$sourcedatadir/wtid/view_cabinet.csv"
append  using "$gendatadir/view_cabinet_pres.dta"
save "$gendatadir/view_cabinet.dta", replace

clear
insheet using "$sourcedatadir/wtid/mpd_2013-01.csv"
encode country, gen(ctry)
drop if country == ""
drop if gdppc == .
sort ctry year
tsset ctry year
gen madd_incgrowthmean     = (gdppc - l.gdppc)/l.gdppc
save "$gendatadir/maddison.dta", replace

do "$scriptsdir/wtid_party_pm_durations.do"
do "$scriptsdir/wtid_party_system_features.do"
do "$scriptsdir/wtid_party_incumbent_voteshare.do"


// Start loading up the data to use.
clear
use "$gendatadir/wtid_processed.dta"
merge 1:1 country year using "$gendatadir/maddison.dta", nogen

gen country_name = country
replace country_name = "United Kingdom" if country_name == "UK"
replace country_name = "United States" if country_name == "USA"
merge 1:1 country_name year using "$gendatadir/party_incumbent_voteshare.dta", keep(master match) nogen

encode country, gen(ctrytemp)
sort ctrytemp year
tsset ctrytemp year
gen election_date = edate
gen election_year_weight = doy(election_date)/365
foreach varn of varlist wtid_incgrowth* madd_inc* share* {
  gen orig_`varn' = `varn'
  replace `varn' = election_year_weight * orig_`varn' + (1-election_year_weight) * l.orig_`varn' if election_year_weight != .
  // The previous line introduces about 10 missing values where the lagged value is not available.
  // Better to maximize N than stick to the weighting scheme for these cases.
  replace `varn' = orig_`varn' if `varn' == . & orig_`varn' != .
  *replace `varn' = l.`varn' if halfyear(election_date) == 1
}
drop ctrytemp


drop if edate == .

merge 1:1 country_name edate using "$gendatadir/party_pm_durations.dta", keep(master match) nogen
merge 1:1 country_name edate using "$gendatadir/party_system_features.dta", keep(master match) nogen

drop ctry
encode country, gen(ctry)
drop if ctry == .
sort ctry edate
by ctry: gen electioncount = _n
sort ctry electioncount
tsset ctry electioncount


// Generate a logged version to allow for diminishing marginal effects of incumbency
gen logcumpm = log(cum_pm_duration)

replace country = "UK" if country == "United Kingdom"
replace country = "USA" if country == "United States"
merge 1:1 country year using "$sourcedatadir/wtid/CPDSI1960-2011stata12_ger.dta"
encode country, gen(ctryarm)
sort ctryarm year
tsset ctryarm year
drop _merge
gen inflation_l1 = l.inflation
gen st_unemp_l1 = l.st_unemp
gen debt_l1 = l.debt
gen debt_l2 = l2.debt
gen debt_l3 = l3.debt
gen deficit_l1 = l.deficit
gen deficit_l2 = l2.deficit
gen deficit_l3 = l3.deficit
gen deficit_d1   = d.deficit
gen deficit_d1l1 = d.l.deficit
gen deficit_d1l2 = d.l2.deficit
gen deficit_d1l3 = d.l3.deficit


replace country = "United Kingdom" if country == "UK"
replace country = "United States" if country == "USA"
merge 1:1 country year using "$gendatadir/oecd_uer.dta", keep(master match) nogen
replace country = "UK" if country == "United Kingdom"
replace country = "USA" if country == "United States"

drop ctry
encode country, gen(ctry)
sort ctry electioncount
tsset ctry electioncount

gen uer_d1 = d.uer

gen share10_x_T10g1    = share10 * wtid_incgrowthtop10
gen share10_x_T5g1     = share10 * wtid_incgrowthtop5
gen share10_x_T1g1     = share10 * wtid_incgrowthtop1

gen share5_x_T10g1     = share5 * wtid_incgrowthtop10
gen share5_x_T5g1      = share5 * wtid_incgrowthtop5
gen share5_x_T1g1      = share5 * wtid_incgrowthtop1

gen share1_x_T10g1     = share1  * wtid_incgrowthtop10
gen share1_x_T5g1      = share1  * wtid_incgrowthtop5
gen share1_x_T1g1      = share1  * wtid_incgrowthtop1

gen share01_x_T01g1    = share01 * wtid_incgrowthtop01


// Follow Tavits2007a to calculate some clarity of responsibility measures.  This excludes cabinet committee measures as we don't have them.
egen    max_effnumparties = max(effnumparties)
gen     std_effnumparties = effnumparties /max_effnumparties 
egen    max_cum_pm_duration = max(cum_pm_duration)
gen     std_cum_pm_duration = cum_pm_duration/max_cum_pm_duration 
gen     clarity2 = (1-std_effnumparties) + (1-incumbent_minority) + (std_cum_pm_duration)
replace clarity2 = clarity2 / 3

gen  clarity2_x_Mg1   = clarity2 * madd_incgrowthmean
gen  clarity2_x_MWg1   = clarity2 * wtid_incgrowthmean
gen  clarity2_x_B90g1 = clarity2 * wtid_incgrowthb90

// Remove older observations on suggestion of a reviewer
keep if year >= 1945

// For easier/nicer embedding in the estout tables
gen Ctry = ctry
gen Party = party_id
drop if Party == .

label variable pm_vote_share_d1		"$ \Delta VoteShare^{PM}_{i,e} $"
label variable pm_vote_share		"$ VoteShare^{PM}_{i,e} $"
label variable pm_vote_share_l1		"$ VoteShare^{PM}_{i,e-1} $"
label variable pm_vote_share_d1l1	"$ \Delta VoteShare^{PM}_{i,e-1} $"
label variable cum_pm_duration		"$ Tenure_{i,t} $"
label variable logcumpm				"$ \log Tenure_{i,t} $"

label variable wtid_incgrowthtop01	"$ Growth^{T0.1M}_{i,t} $"
label variable wtid_incgrowthtop05	"$ Growth^{T0.5M}_{i,t} $"
label variable wtid_incgrowthtop1	"$ Growth^{T1M}_{i,t} $"
label variable wtid_incgrowthtop5	"$ Growth^{T5M}_{i,t} $"
label variable wtid_incgrowthtop10	"$ Growth^{T10M}_{i,t} $"
label variable wtid_incgrowthmean	"$ Growth^{M}_{i,t} $"
label variable wtid_incgrowthb90	"$ Growth^{B90}_{i,t} $"
label variable madd_incgrowthmean	"$ Growth^{M}_{i,t} $"

label variable share10				"$ Share^{T10}_{i,t} $"
label variable share10_x_T10g1	 	"$ Share^{T10}_{i,t} \times Growth^{T10M}_{i,t} $"
label variable share5				"$ Share^{T5}_{i,t} $"
label variable share5_x_T5g1	 	"$ Share^{T5}_{i,t} \times Growth^{T5M}_{i,t} $"
label variable share1				"$ Share^{T1}_{i,t} $"
label variable share1_x_T1g1	 	"$ Share^{T1}_{i,t} \times Growth^{T1M}_{i,t} $"
label variable share01				"$ Share^{T0.1}_{i,t} $"
label variable share01_x_T01g1	 	"$ Share^{T0.1}_{i,t} \times Growth^{T0.1M}_{i,t} $"

label variable year					"$ Year_{t} $"

label variable clarity2				"$ Clarity_{i,t} $"
label variable clarity2_x_Mg1		"$ Clarity_{i,t} \times Growth^{M}_{i,t} $"
label variable clarity2_x_MWg1		"$ Clarity_{i,t} \times Growth^{M}_{i,t} $"
label variable clarity2_x_B90g1		"$ Clarity_{i,t} \times Growth^{B90}_{i,t} $"

label variable uer					"$ Unemployment_{i,t} $"
label variable uer_d1				"$ \Delta Unemployment_{i,t} $"

label variable inflation			"$ Inflation_{i,t} $"

label variable deficit				"$ Deficit_{i,t} $"
label variable deficit_l1			"$ Deficit_{i,t-1} $"
label variable deficit_l2			"$ Deficit_{i,t-2} $"
label variable deficit_d1			"$ \Delta Deficit_{i,t} $"
label variable deficit_d1l1			"$ \Delta Deficit_{i,t-1} $"
label variable deficit_d1l2			"$ \Delta Deficit_{i,t-2} $"


do "$scriptsdir/wtid_datacheck.do"


global estout_options         label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(r2   bic N N_clust clustvar           , fmt(2 0 0) labels("$ R^2 $" "BIC" "N" "N countries" "Std. errors clustered")) cells( " b( fmt(2) ) p(par fmt(2)) " )       mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )
global estout_options_w_se    label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(r2   bic N N_clust clustvar           , fmt(2 0 0) labels("$ R^2 $" "BIC" "N" "N countries" "Std. errors clustered")) cells( " b( fmt(2) ) se(fmt(2)) p(fmt(2))" ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )
global estout_options_w_jack  label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(r2   bic N N_clust clustvar setype   , fmt(2 0 0) labels("$ R^2 $" "BIC" "N" "N countries" "Std. errors clustered" "Std. error type")) cells( " b( fmt(2) ) se(fmt(2)) p(fmt(2))" ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )
global estout_options_w_se_xt label replace style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline") varlabels(_cons "$ Intercept $") stats(r2_o bic N N_clust clustvar model ivar, fmt(2 0 0) labels("$ R^2 $" "BIC" "N" "N countries" "Std. errors clustered" "Unit effects" "Unit")) cells( " b( fmt(2) ) se(fmt(2)) p(fmt(2))" ) mlabels("" "" "" "" "" "" "", numbers span prefix(\multicolumn{@span}{c}{) suffix(}) )


//// Section of models that we have decided to include in the main text of CBEVComparative

reg pm_vote_share_d1 pm_vote_share_l1 logcumpm        madd_incgrowthmean wtid_incgrowthtop5, cluster(Ctry)
estimates store MeanParsi
reg pm_vote_share_d1 pm_vote_share_l1 logcumpm        madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1, cluster(Ctry)
estimates store MeanShare5
reg pm_vote_share_d1 pm_vote_share_l1 cum_pm_duration madd_incgrowthmean wtid_incgrowthtop5, cluster(Ctry)
estimates store MeanParsiAlt
reg pm_vote_share_d1 pm_vote_share_l1 cum_pm_duration madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1, cluster(Ctry)
estimates store MeanShare5Alt
*reg pm_vote_share_d1 pm_vote_share_l1 logcumpm wtid_incgrowthb90  wtid_incgrowthtop5, cluster(Ctry)
*estimates store B90Parsi
*reg pm_vote_share_d1 pm_vote_share_l1 logcumpm wtid_incgrowthb90  wtid_incgrowthtop5 share5 share5_x_T5g1, cluster(Ctry)
*estimates store B90Share5

reg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop1, cluster(Ctry)
estimates store MeanParsiT1
reg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop1 share1 share1_x_T1g1, cluster(Ctry)
estimates store MeanT1Share1

*estout	MeanParsiT01 MeanT01Share01	using "dboutput/wtid_cbevcomp_supp_t01.tex", $estout_options_w_se order(pm_vote_share_l1 logcumpm madd_incgrowthmean)
*estout	MeanParsi MeanShare5 	using "dboutput/wtid_cbevcomp_tableA.tex", $estout_options_w_se order(pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1)
estout	MeanParsi MeanShare5 MeanParsiT1 MeanT1Share1	using "$texdir/wtid_cbevcomp_tableA.tex", $estout_options_w_se order(pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1)


*reg pm_vote_share_d1 pm_vote_share_l1 logcumpm wtid_incgrowthmean wtid_incgrowthtop5, cluster(Ctry)
*estimates store MeanParsi
*reg pm_vote_share_d1 pm_vote_share_l1 logcumpm wtid_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1, cluster(Ctry)
*estimates store MeanShare5
*reg pm_vote_share_d1 pm_vote_share_l1 logcumpm wtid_incgrowthb90  wtid_incgrowthtop5, cluster(Ctry)
*estimates store B90Parsi
*reg pm_vote_share_d1 pm_vote_share_l1 logcumpm wtid_incgrowthb90  wtid_incgrowthtop5 share5 share5_x_T5g1, cluster(Ctry)
*estimates store B90Share5
*
*estout	MeanParsi MeanShare5 B90Parsi B90Share5	using "dboutput/wtid_cbevcomp_tableAx.tex", $estout_options_w_se order(pm_vote_share_l1 logcumpm wtid_incgrowthmean wtid_incgrowthb90 wtid_incgrowthtop5 share5 share5_x_T5g1)


// Re-estimate in a way that is margins-friendly
// First create a standardized version of the relevant variables so that we can generate plots that show the effect of a SD change in the variable
norm wtid_incgrowthtop5 wtid_incgrowthtop1 wtid_incgrowthtop01, method(zee)

reg pm_vote_share_d1 pm_vote_share_l1 logcumpm c.madd_incgrowthmean c.zee_wtid_incgrowthtop5##c.share5, cluster(Ctry)
summ share5 if e(sample)
local share5_min = round(r(min))
local share5_max = round(r(max))
margins, dydx(zee_wtid_incgrowthtop5) at(share5=(`share5_min'(3)`share5_max'))
marginsplot, yline(0) recast(line) recastci(rarea) scheme(s2mono) graphregion(color(white)) ciopts(color(*.5)) title("") xtitle("{it:Share}{superscript:T5}") ytitle("Marginal effect of {it:Growth}{superscript:T5}") yscale(range(-60(20)60)) ylabel(-60(20)60) addplot(histogram share5 if e(sample), frequency width(2) scheme(s1mono) start(13) fcolor(none) lcolor(gs12) yaxis(2) yscale(alt) xlabel(13(4)33) ytitle("Number of {it:Share}{superscript:T5} observations", axis(2)) below) legend(off)
graph export "$imagedir/mfx_T5_by_share5.pdf", replace
window manage close graph

reg pm_vote_share_d1 pm_vote_share_l1 logcumpm c.madd_incgrowthmean c.zee_wtid_incgrowthtop1##c.share1, cluster(Ctry)
summ share1 if e(sample)
local share1_min = round(r(min),0.1)
local share1_max = round(r(max),0.1)
margins, dydx(zee_wtid_incgrowthtop1) at(share1=(`share1_min'(.25)`share1_max'))
marginsplot, yline(0) recast(line) recastci(rarea) scheme(s2mono) graphregion(color(white)) ciopts(color(*.5)) title("") xtitle("{it:Share}{superscript:T1}") ytitle("Marginal effect of {it:Growth}{superscript:T1}") yscale(range(-25(5)20)) ylabel(-25(5)20) addplot(histogram share1 if e(sample), frequency width(1.5) scheme(s1mono) start(3.5) fcolor(none) lcolor(gs12) yaxis(2) yscale(alt) xlabel(4(2)18) ytitle("Number of {it:Share}{superscript:T1} observations", axis(2)) below) legend(off)
graph export "$imagedir/mfx_T1_by_share1.pdf", replace
window manage close graph

reg pm_vote_share_d1 pm_vote_share_l1 logcumpm c.madd_incgrowthmean c.zee_wtid_incgrowthtop01##c.share01, cluster(Ctry)
summ share01 if e(sample)
local share01_min = round(r(min),0.1)
local share01_max = round(r(max),0.1)
margins, dydx(zee_wtid_incgrowthtop01) at(share01=(`share01_min'(.25)`share01_max'))
marginsplot, yline(0) recast(line) recastci(rarea) scheme(s2mono) graphregion(color(white)) ciopts(color(*.5)) title("") xtitle("{it:Share}{superscript:T0.1}") ytitle("Marginal effect of {it:Growth}{superscript:T0.1}") yscale(range(-25(5)20)) ylabel(-25(5)20) addplot(histogram share01 if e(sample), frequency width(0.75) scheme(s1mono) start(0.75) fcolor(none) lcolor(gs12) yaxis(2) yscale(alt) xlabel(0.75(0.75)8) ytitle("Number of {it:Share}{superscript:T0.1} observations", axis(2)) below) legend(off)
graph export "$imagedir/mfx_T01_by_share01.pdf", replace
window manage close graph


*tsset ctry year
*scatter share5 year if e(sample),              yline(25) xtitle("Year") ytitle("{it:Share}{superscript:T5}") graphregion(color(white)) ylabel(15(5)35)
*graph export "$imagedir/share5_scatter.pdf", replace
*window manage close graph
*xtline share5 if e(sample) & (country == "UK" | country == "USA" | country == "Sweden" | country == "Canada"),               overlay yline(25) xtitle("Year") ytitle("{it:Share}{superscript:T5}") graphregion(color(white)) ylabel(15(5)35)
*graph export "$imagedir/share5_ukusseca.pdf", replace
*window manage close graph
*xtline share5 if e(sample) & (country == "Australia" | country == "New Zealand" | country == "Japan"),                       overlay yline(25) xtitle("Year") ytitle("{it:Share}{superscript:T5}") graphregion(color(white)) ylabel(15(5)35)
*graph export "$imagedir/share5_aunzjp.pdf", replace
*window manage close graph
*xtline share5 if e(sample) & (country == "Denmark" | country == "Germany" | country == "Netherlands" | country == "Norway"), overlay yline(25) xtitle("Year") ytitle("{it:Share}{superscript:T5}") graphregion(color(white)) ylabel(15(5)35)
*graph export "$imagedir/share5_dkdenlno.pdf", replace
*window manage close graph
*xtline share5 if e(sample) & (country == "France" | country == "Italy" | country == "Spain" | country == "Portugal"),        overlay yline(25) xtitle("Year") ytitle("{it:Share}{superscript:T5}") graphregion(color(white)) ylabel(15(5)35)
*graph export "$imagedir/share5_fritespt.pdf", replace
*window manage close graph
*tsset ctry electioncount


gen     ByPeriod = .
replace ByPeriod = 1955 if _n == 1
replace ByPeriod = 1965 if _n == 2
replace ByPeriod = 1975 if _n == 3
replace ByPeriod = 1985 if _n == 4
replace ByPeriod = 1995 if _n == 5
replace ByPeriod = 2005 if _n == 6
gen     ByPeriodCoef = .
gen     ByPeriodCoefl90 = .
gen     ByPeriodCoefu90 = .
gen     ByPeriodN = .
gen     ByPeriodShare5 = .

local base_spec pm_vote_share_d1 wtid_incgrowthtop5 pm_vote_share_l1 logcumpm madd_incgrowthmean
*local base_spec pm_vote_share_d1 wtid_incgrowthtop5 pm_vote_share_l1 logcumpm wtid_incgrowthmean

reg `base_spec' if year > 1945 & year < 1966, cluster(Ctry)
estimates store Top5TenureY4565
lincom _b[wtid_incgrowthtop5]
replace ByPeriodCoef = r(estimate) if _n == 1
local critval = invttail(r(df),.05)
replace ByPeriodCoefl90 = r(estimate) - `critval'*r(se) if _n == 1
replace ByPeriodCoefu90 = r(estimate) + `critval'*r(se) if _n == 1
replace ByPeriodN = e(N) if _n == 1
quietly summ share5 if e(sample)
replace ByPeriodShare5 = r(mean) if _n == 1

reg `base_spec' if year > 1955 & year < 1976, cluster(Ctry)
estimates store Top5TenureY5575
lincom _b[wtid_incgrowthtop5]
replace ByPeriodCoef = r(estimate) if _n == 2
local critval = invttail(r(df),.05)
replace ByPeriodCoefl90 = r(estimate) - `critval'*r(se) if _n == 2
replace ByPeriodCoefu90 = r(estimate) + `critval'*r(se) if _n == 2
replace ByPeriodN = e(N) if _n == 2
quietly summ share5 if e(sample)
replace ByPeriodShare5 = r(mean) if _n == 2

reg `base_spec' if year > 1965 & year < 1986, cluster(Ctry)
estimates store Top5TenureY6585
lincom _b[wtid_incgrowthtop5]
replace ByPeriodCoef = r(estimate) if _n == 3
local critval = invttail(r(df),.05)
replace ByPeriodCoefl90 = r(estimate) - `critval'*r(se) if _n == 3
replace ByPeriodCoefu90 = r(estimate) + `critval'*r(se) if _n == 3
replace ByPeriodN = e(N) if _n == 3
quietly summ share5 if e(sample)
replace ByPeriodShare5 = r(mean) if _n == 3

reg `base_spec' if year > 1975 & year < 1996, cluster(Ctry)
estimates store Top5TenureY7595
lincom _b[wtid_incgrowthtop5]
replace ByPeriodCoef = r(estimate) if _n == 4
local critval = invttail(r(df),.05)
replace ByPeriodCoefl90 = r(estimate) - `critval'*r(se) if _n == 4
replace ByPeriodCoefu90 = r(estimate) + `critval'*r(se) if _n == 4
replace ByPeriodN = e(N) if _n == 4
quietly summ share5 if e(sample)
replace ByPeriodShare5 = r(mean) if _n == 4

reg `base_spec' if year > 1985 & year < 1996, cluster(Ctry)
estimates store Top5TenureY8505
lincom _b[wtid_incgrowthtop5]
replace ByPeriodCoef = r(estimate) if _n == 5
local critval = invttail(r(df),.05)
replace ByPeriodCoefl90 = r(estimate) - `critval'*r(se) if _n == 5
replace ByPeriodCoefu90 = r(estimate) + `critval'*r(se) if _n == 5
replace ByPeriodN = e(N) if _n == 5
quietly summ share5 if e(sample)
replace ByPeriodShare5 = r(mean) if _n == 5

reg `base_spec' if year > 1995              , cluster(Ctry)
estimates store Top5TenureY95
lincom _b[wtid_incgrowthtop5]
replace ByPeriodCoef = r(estimate) if _n == 6
local critval = invttail(r(df),.05)
replace ByPeriodCoefl90 = r(estimate) - `critval'*r(se) if _n == 6
replace ByPeriodCoefu90 = r(estimate) + `critval'*r(se) if _n == 6
replace ByPeriodN = e(N) if _n == 6
quietly summ share5 if e(sample)
replace ByPeriodShare5 = r(mean) if _n == 6

*twoway (bar ByPeriodN ByPeriod, yaxis(2) ytitle("Sample size", axis(2)) ylabel(0(20)80, axis(2)) color(gs13)) (rcap ByPeriodCoefl90 ByPeriodCoefu90 ByPeriod, yaxis(1)) (scatter ByPeriodCoef ByPeriod, yaxis(1)), legend(off) xlabel(1955(10)2005) ytitle("Top-income coefficient and 90% confidence interval", axis(1)) xtitle("Year") scheme(s2mono)
*graph export "$imagedir/wtid_byperiod_coefs.pdf", replace
*window manage close graph

gen ByPeriodNegShare5 = 100 - ByPeriodShare5
*twoway (line ByPeriodNegShare5 ByPeriod, yaxis(2) ytitle("Income Share to non-top-5", axis(2)) ylabel(75(1)83, axis(2)) color(gs13)) (rcap ByPeriodCoefl90 ByPeriodCoefu90 ByPeriod, yaxis(1)) (scatter ByPeriodCoef ByPeriod, yaxis(1)), legend(off) xlabel(1955(10)2005) ytitle("Top-income coefficient and 90% confidence interval", axis(1)) xtitle("Year") scheme(s2mono) graphregion(color(white)) title("")
twoway (rcap ByPeriodCoefl90 ByPeriodCoefu90 ByPeriod, yaxis(1)) (scatter ByPeriodCoef ByPeriod, yaxis(1)) (line ByPeriodNegShare5 ByPeriod, lpattern("dash") yaxis(2) ytitle("Income Share to bottom-95%", axis(2)) ylabel(75(1)85, axis(2)) color(gs13)), xlabel(1955(10)2005) ytitle("Top-income coefficient and 90% confidence interval", axis(1)) xtitle("Year") scheme(s2mono) graphregion(color(white)) title("") legend(order(2 "T5 coefficient estimate" 3 "Income share to B95"))
graph export "$imagedir/wtid_byperiod_coefs_share5.pdf", replace
twoway (bar ByPeriodNegShare5 ByPeriod, barwidth(5) yaxis(2) ytitle("Income Share to bottom-95%", axis(2)) ylabel(75(2)85, axis(2)) color(gs13)) (rcap ByPeriodCoefl90 ByPeriodCoefu90 ByPeriod, yaxis(1)) (scatter ByPeriodCoef ByPeriod, yaxis(1)), legend(off) xlabel(1955(10)2005) ytitle("Top-income coefficient and 90% confidence interval", axis(1)) xtitle("Year") scheme(s2mono) graphregion(color(white)) title("")
graph export "$imagedir/wtid_byperiod_coefs_share5b.pdf", replace
window manage close graph




//// Section of models that we present in supplementary material

reg pm_vote_share_d1 pm_vote_share_l1 cum_pm_duration madd_incgrowthmean wtid_incgrowthtop5, cluster(Ctry)
estimates store MeanParsiAlt
reg pm_vote_share_d1 pm_vote_share_l1 cum_pm_duration madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1, cluster(Ctry)
estimates store MeanShare5Alt
*reg pm_vote_share_d1 pm_vote_share_l1 logcumpm wtid_incgrowthb90  wtid_incgrowthtop5, cluster(Ctry)
*estimates store B90Parsi
*reg pm_vote_share_d1 pm_vote_share_l1 logcumpm wtid_incgrowthb90  wtid_incgrowthtop5 share5 share5_x_T5g1, cluster(Ctry)
*estimates store B90Share5

estout	MeanParsiAlt MeanShare5Alt 	using "$texdir/wtid_cbevcomp_tableB.tex", $estout_options_w_se order(pm_vote_share_l1 cum_pm_duration madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1)



jackknife, cluster(Ctry): reg pm_vote_share_d1 pm_vote_share_l1 logcumpm        madd_incgrowthmean wtid_incgrowthtop5
estadd local setype "Jknife"
estimates store MeanParsiJack
jackknife, cluster(Ctry): reg pm_vote_share_d1 pm_vote_share_l1 logcumpm        madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1
estadd local setype "Jknife"
estimates store MeanShare5Jack
*jackknife, cluster(Ctry): reg pm_vote_share_d1 pm_vote_share_l1 cum_pm_duration madd_incgrowthmean wtid_incgrowthtop5
*estadd local setype "Jknife"
*estimates store MeanParsiAltJack
*jackknife, cluster(Ctry): reg pm_vote_share_d1 pm_vote_share_l1 cum_pm_duration madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1
*estadd local setype "Jknife"
*estimates store MeanShare5AltJack

estout	MeanParsiJack MeanShare5Jack 	using "$texdir/wtid_cbevcomp_tableAJack.tex", $estout_options_w_jack order(pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1)


reg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop10, cluster(Ctry)
estimates store MeanParsiT10
reg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop10 share10 share10_x_T10g1, cluster(Ctry)
estimates store MeanT10Share10
reg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop01, cluster(Ctry)
estimates store MeanParsiT01
reg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop01 share01 share01_x_T01g1, cluster(Ctry)
estimates store MeanT01Share01

estout	MeanParsiT10 MeanT10Share10 MeanParsiT01 MeanT01Share01	using "$texdir/wtid_cbevcomp_supp_t1001.tex", $estout_options_w_se order(pm_vote_share_l1 logcumpm madd_incgrowthmean)


reg pm_vote_share_d1 pm_vote_share_l1 logcumpm uer madd_incgrowthmean wtid_incgrowthtop5, cluster(Ctry)
estimates store MeanParsiUER
reg pm_vote_share_d1 pm_vote_share_l1 logcumpm uer madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1, cluster(Ctry)
estimates store MeanShare5UER
reg pm_vote_share_d1 pm_vote_share_l1 logcumpm uer uer_d1 inflation madd_incgrowthmean wtid_incgrowthtop5, cluster(Ctry)
estimates store MeanParsiEcon
reg pm_vote_share_d1 pm_vote_share_l1 logcumpm uer uer_d1 inflation madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1, cluster(Ctry)
estimates store MeanShare5Econ

estout	MeanParsiUER MeanShare5UER MeanParsiEcon MeanShare5Econ	using "$texdir/wtid_cbevcomp_supp_uer.tex", $estout_options_w_se order(pm_vote_share_l1 logcumpm uer madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1)


// For easier/nicer embedding in the estout tables
tsset Ctry year
xtreg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5, cluster(Ctry) fe
estimates store MeanParsiCtryFE
xtreg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1, cluster(Ctry) fe
estimates store MeanShare5CtryFE
xtreg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5, cluster(Ctry) re
estimates store MeanParsiCtryRE
xtreg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1, cluster(Ctry) re
estimates store MeanShare5CtryRE
tsset, clear

estout	MeanParsiCtryFE MeanShare5CtryFE MeanParsiCtryRE MeanShare5CtryRE	using "$texdir/wtid_cbevcomp_supp_ctryfere.tex", $estout_options_w_se_xt order(pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1)


// For easier/nicer embedding in the estout tables
tsset Party year
xtreg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5, cluster(Ctry) fe
estimates store MeanParsiPartyFE
xtreg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1, cluster(Ctry) fe
estimates store MeanShare5PartyFE
xtreg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5, cluster(Ctry) re
estimates store MeanParsiPartyRE
xtreg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1, cluster(Ctry) re
estimates store MeanShare5PartyRE
tsset, clear

estout	MeanParsiPartyFE MeanShare5PartyFE MeanParsiPartyRE MeanShare5PartyRE	using "$texdir/wtid_cbevcomp_supp_partyfere.tex", $estout_options_w_se_xt order(pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1)


reg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5 year, cluster(Ctry)
estimates store MeanParsiYear
reg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1 year, cluster(Ctry)
estimates store MeanShare5Year
reg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean clarity2 clarity2_x_Mg1 wtid_incgrowthtop5, cluster(Ctry)
estimates store MeanParsiClarity
reg pm_vote_share_d1 pm_vote_share_l1 logcumpm madd_incgrowthmean clarity2 clarity2_x_Mg1 wtid_incgrowthtop5 share5 share5_x_T5g1, cluster(Ctry)
estimates store MeanShare5Clarity

estout	MeanParsiYear MeanShare5Year MeanParsiClarity MeanShare5Clarity	using "$texdir/wtid_cbevcomp_supp_year_clarity.tex", $estout_options_w_se order(pm_vote_share_l1 logcumpm madd_incgrowthmean wtid_incgrowthtop5 share5 share5_x_T5g1)



estimates restore MeanParsi
gen sample_MeanParsi = 1 if e(sample)
estimates restore MeanParsiT1
gen sample_MeanParsiT1 = 1 if e(sample)
sutex pm_vote_share pm_vote_share_d1 cum_pm_duration madd_incgrowthmean wtid_incgrowthtop5 share5 wtid_incgrowthtop1 share1 if sample_MeanParsi == 1 | sample_MeanParsiT1 == 1, nocheck labels minmax digits(2) key("tab:WTID:VarSumm")

gen wtid_incgrowthtop5_100 = wtid_incgrowthtop5 * 100
gen madd_incgrowthmean_100 = madd_incgrowthmean * 100
twoway (function y=x, range(-15 25) text(19 16 "y=x", color(gs6))) (scatter wtid_incgrowthtop5_100 madd_incgrowthmean_100 if sample_MeanParsi == 1, msymbol(oh) yline(0, lstyle(dot)) xline(0, lstyle(dot))), scheme(s1mono) xtitle("Mean income growth (%)") ytitle("Top-5% income growth (%)") xlabel(-15(5)25) ylabel(-15(5)25) legend(off)
graph export "$imagedir/scatter_mean_t5.pdf", replace
window manage close graph

cor wtid_incgrowthtop5_100 madd_incgrowthmean_100 if sample_MeanParsi == 1
local rho =  string(r(rho), "%9.2fc")
capture {
  file open  rhofile using "$texdir/rho_mean_t5.tex", write replace
  file write rhofile "`rho'"
  file close rhofile
}
